-
Notifications
You must be signed in to change notification settings - Fork 15
/
three_sum.py
36 lines (30 loc) · 930 Bytes
/
three_sum.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# Github: Shantanugupta1118
# DAY 16 of DAY 100
# 3SUMS - Interviewbits/Leetcode
def main():
nums = list(map(int, input().split()))
res = []
n = len(nums)-1
nums.sort()
for i in range(n-1):
if i > 0 and nums[i] == nums[i-1]: continue
j, k = i+1, n
while j < k:
temp = nums[i] + nums[j] + nums[k]
if not temp:
res.append([nums[i], nums[j], nums[k]])
while j < k and nums[j] == nums[j+1]:
j += 1
print(nums[j], nums[j+1])
while j < k and nums[k] == nums[k-1]:
k -= 1
print(nums[k], nums[k-1])
print(nums[i], nums[j], nums[k])
j += 1
k -= 1
elif temp < 0:
j += 1
else:
k -= 1
print(res)
main()